package com.ruoyi.firemen.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.firemen.domain.FmPFinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.firemen.domain.FmBasePhysicalFitnessItems;
import com.ruoyi.firemen.mapper.FmPhysicalFitnessinfoMapper;
import com.ruoyi.firemen.service.IFmPhysicalFitnessinfoService;

/**
 * 基础体能考核Service业务层处理
 * 
 * @author zmc
 * @date 2024-09-25
 */
@Service
public class FmPhysicalFitnessinfoServiceImpl implements IFmPhysicalFitnessinfoService 
{
    @Autowired
    private FmPhysicalFitnessinfoMapper fmPhysicalFitnessinfoMapper;

    /**
     * 查询基础体能考核
     * 
     * @param id 基础体能考核主键
     * @return 基础体能考核
     */
    @Override
    public FmPFinfo selectFmPhysicalFitnessinfoById(Long id)
    {
        return fmPhysicalFitnessinfoMapper.selectFmPhysicalFitnessinfoById(id);
    }

    /**
     * 查询基础体能考核列表
     * 
     * @param fmPhysicalFitnessinfo 基础体能考核
     * @return 基础体能考核
     */
    @Override
    public List<FmPFinfo> selectFmPhysicalFitnessinfoList(FmPFinfo fmPhysicalFitnessinfo)
    {
        return fmPhysicalFitnessinfoMapper.selectFmPhysicalFitnessinfoList(fmPhysicalFitnessinfo);
    }

    /**
     * 新增基础体能考核
     * 
     * @param fmPhysicalFitnessinfo 基础体能考核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFmPhysicalFitnessinfo(FmPFinfo fmPhysicalFitnessinfo)
    {
        int rows = fmPhysicalFitnessinfoMapper.insertFmPhysicalFitnessinfo(fmPhysicalFitnessinfo);
        insertFmBasePhysicalFitnessItems(fmPhysicalFitnessinfo,"Base");
        return rows;
    }

    /**
     * 新增基础体能考核
     *
     * @param fmPhysicalFitnessinfo 基础体能考核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFmPhysicalFitnessinfoT(FmPFinfo fmPhysicalFitnessinfo)
    {
        int rows = fmPhysicalFitnessinfoMapper.insertFmPhysicalFitnessinfo(fmPhysicalFitnessinfo);
        insertFmBasePhysicalFitnessItems(fmPhysicalFitnessinfo,"Applied");
        return rows;
    }

    /**
     * 修改基础体能考核
     * 
     * @param fmPhysicalFitnessinfo 基础体能考核
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFmPhysicalFitnessinfo(FmPFinfo fmPhysicalFitnessinfo)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("firefighterId", fmPhysicalFitnessinfo.getId());
        params.put("fitnessType", "Base");

        fmPhysicalFitnessinfoMapper.deleteFmBasePhysicalFitnessItemsByFirefighterId(params);
        insertFmBasePhysicalFitnessItems(fmPhysicalFitnessinfo,"Base");

        return fmPhysicalFitnessinfoMapper.updateFmPhysicalFitnessinfo(fmPhysicalFitnessinfo);
    }

    /**
     * 修改基础体能考核
     *
     * @param fmPhysicalFitnessinfo 应用体能考核
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFmPhysicalFitnessinfoT(FmPFinfo fmPhysicalFitnessinfo)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("firefighterId", fmPhysicalFitnessinfo.getId());
        params.put("fitnessType", "Applied");
        fmPhysicalFitnessinfoMapper.deleteFmBasePhysicalFitnessItemsByFirefighterId(params);
        insertFmBasePhysicalFitnessItems(fmPhysicalFitnessinfo,"Applied");
        return fmPhysicalFitnessinfoMapper.updateFmPhysicalFitnessinfo(fmPhysicalFitnessinfo);
    }

    /**
     * 批量删除基础体能考核
     * 
     * @param ids 需要删除的基础体能考核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFmPhysicalFitnessinfoByIds(Long[] ids)
    {
        fmPhysicalFitnessinfoMapper.deleteFmBasePhysicalFitnessItemsByFirefighterIds(ids);
        return fmPhysicalFitnessinfoMapper.deleteFmPhysicalFitnessinfoByIds(ids);
    }

    /**
     * 删除基础体能考核信息
     * 
     * @param id 基础体能考核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFmPhysicalFitnessinfoById(Long id)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("firefighterId", id);
        params.put("fitnessType", "Applied");
        fmPhysicalFitnessinfoMapper.deleteFmBasePhysicalFitnessItemsByFirefighterId(params);

        return fmPhysicalFitnessinfoMapper.deleteFmPhysicalFitnessinfoById(id);
    }

    /**
     * 新增体能单项成绩信息
     * 
     * @param fmPhysicalFitnessinfo 基础体能考核对象
     */
    public void insertFmBasePhysicalFitnessItems(FmPFinfo fmPhysicalFitnessinfo, String type)
    {
        List<FmBasePhysicalFitnessItems> fmBasePhysicalFitnessItemsList = fmPhysicalFitnessinfo.getFmBasePhysicalFitnessItemsList();
        Long id = fmPhysicalFitnessinfo.getId();
        if (StringUtils.isNotNull(fmBasePhysicalFitnessItemsList))
        {
            List<FmBasePhysicalFitnessItems> list = new ArrayList<FmBasePhysicalFitnessItems>();
            for (FmBasePhysicalFitnessItems fmBasePhysicalFitnessItems : fmBasePhysicalFitnessItemsList)
            {
                fmBasePhysicalFitnessItems.setFirefighterId(id);
                list.add(fmBasePhysicalFitnessItems);
            }
            if (list.size() > 0)
            {
                if (type.equals("Base")){
                    fmPhysicalFitnessinfoMapper.batchFmBasePhysicalFitnessItems(list);
                }else if (type.equals("Applied")){
                    fmPhysicalFitnessinfoMapper.batchFmBasePhysicalFitnessItemsApplied(list);

                }

            }
        }
    }
}
